package com.five.second_shop.controller;

import com.five.second_shop.entity.Commodity;
import com.five.second_shop.entity.Notices;
import com.five.second_shop.entity.Order;
import com.five.second_shop.service.NoticesService;
import com.five.second_shop.service.OrderService;
import com.five.second_shop.util.KeyUtil;
import com.five.second_shop.util.StatusCode;
import com.five.second_shop.vo.LayuiPageVo;
import com.five.second_shop.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

//消息通知控制器
@Controller
public class NoticesController {
    @Autowired
    private NoticesService noticesService;

    @Autowired
    private OrderService orderService;


    /**
     * 用户查看通知消息后
     * 1.前端传入通知id（id）
     * 2.将其设置为已读
     */
    @ResponseBody
    @PutMapping("/notices/look/{id}")
    public ResultVo LookNoticesById(@PathVariable("id") String id) {
        Integer i = noticesService.updateNoticesById(id);
        if (i == 1) {
            return new ResultVo(true, StatusCode.OK, "设置成功");
        }
        return new ResultVo(true, StatusCode.ERROR, "设置失败");
    }

    /**
     * 查询前10条公告
     **/
    @ResponseBody
    @GetMapping("/notices/queryNotices")
    public ResultVo queryNotices(HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        List<Notices> noticesList = noticesService.queryNotices(userid);
        return new ResultVo(true, StatusCode.OK, "查询成功", noticesList);
    }

    /**
     * 取消新通知标志
     * 用户点击查看最新通知后会将所有通知设置为非最新通知
     */
    @ResponseBody
    @GetMapping("/notices/cancelLatest")
    public ResultVo CancelLatest(HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        Integer i = noticesService.CancelLatest(userid);
        if (i == 1) {
            return new ResultVo(true, StatusCode.OK, "设置成功");
        }
        return new ResultVo(true, StatusCode.ERROR, "设置失败");
    }

    /**
     * 分类分页查询用户所有通知消息
     * 1.前端传入消息通知类型（tpname）
     * 2.session中获取用户id（userid）
     * 3.返回分页数据
     */
    @ResponseBody
    @GetMapping("/notices/queryall")
    public LayuiPageVo queryallSold(int limit, int page, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        List<Notices> noticesList = noticesService.queryAllNotices((page - 1) * limit, limit, userid);
        Integer dataNumber = noticesService.queryNoticesCount(userid);
        return new LayuiPageVo("", 0, dataNumber, noticesList);
    }

    /**
     * 购买成功通知卖家发货
     * @param commmid
     * @param userid
     * @return
     */
    @ResponseBody
    @GetMapping("/notice/toUser/{commid}/{userid}")
    public ResultVo toUser(@PathVariable("commid") String commmid,@PathVariable("userid") String userid){
        List<Order> orders = orderService.LookOrder(new Order().setCommid(commmid).setUserid(userid));
        Commodity commodity = orders.get(0).getCommodity();
         Notices notices =  new Notices().setId(KeyUtil.genUniqueKey())
                                        /*由一对一查询找出这个商品的发布者id*/
                                        .setTpname("发货通知")
                                        .setUserid(commodity.getUserid())
                                        .setWhys("您的商品 <a href='#' style=\"color:#08bf91\" target=\"_blank\" >" + commodity.getCommname() + "</a>已有买家下单快去发货。订单页面搜索"+orders.get(0).getOrderid()+"查看详细信息");
            noticesService.insertNotices(notices);
            return new ResultVo(true, StatusCode.OK, "已通知买家发货，请耐心等待");
    }

}
